package com.freight.tws.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.constant.DictConstants;
import com.freight.common.core.constant.WebsocketConstants;
import com.freight.common.core.exception.ServiceException;
import com.freight.common.security.utils.SecurityUtils;
import com.freight.common.wlhyupload.WlhyUpload;
import com.freight.tws.api.domain.Driver;
import com.freight.tws.api.domain.PtQy;
import com.freight.tws.api.domain.Vehicle;
import com.freight.tws.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.freight.common.core.web.controller.BaseServiceImpl;
import com.freight.tws.mapper.VehicleMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-20
 */
@Service
public class VehicleServiceImpl extends BaseServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private IDriverService driverService;

    @Autowired
    private ITwsNoticeService twsNoticeService;

    @Autowired
    private IPtQyService ptQyService;

    /**
     * 新增车辆
     * @param vehicle
     * @return
     */
    @Override
    @Transactional
    public boolean add(Vehicle vehicle) {
        // 查询当前司机是否添加过该车辆
        Long userId = SecurityUtils.getUserId();
        Vehicle selectOne = vehicleMapper.selectOne(new LambdaQueryWrapper<Vehicle>()
                .eq(Vehicle::getDriverId, userId)
                .eq(Vehicle::getVehicleNumber, vehicle.getVehicleNumber()));
        if(selectOne != null) {
            throw new ServiceException("您已添加该车辆");
        }
        vehicle.setDriverId(userId);
        vehicle.setIsSendToPro(DictConstants.IS_SEND_TO_PRO.WSC.getKey());
        // 保存到业务公告消息表（包含websocket通知）
        twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE.getName());
        return this.save(vehicle);
    }

    /**
     * 车辆审核
     * @param vehicle
     * @return
     */
    @Override
    @Transactional
    public int cert(Vehicle vehicle) {
        Vehicle byId = vehicleMapper.selectById(vehicle.getId());
        // 如果状态不是在审核中
        if(!DictConstants.IS_CERT.SHZ.getKey().equals(byId.getIsCert())) {
            throw new ServiceException("审核失败");
        }
        Driver driver = driverService.getById(byId.getDriverId());
        // 审核通过
        if(DictConstants.IS_CERT.YRZ.getKey().equals(vehicle.getIsCert())) {
            // 保存到业务公告消息表（包含websocket通知）(司机的手机号即为登录用户名)
            twsNoticeService.newTwsNotice(driver.getTelephone(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE_PASS.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE_PASS.getName());
        } else if(DictConstants.IS_CERT.RZSB.getKey().equals(vehicle.getIsCert())) {
            // 保存到业务公告消息表（包含websocket通知）(司机的手机号即为登录用户名)
            twsNoticeService.newTwsNotice(driver.getTelephone(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE_FAIL.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_VEHICLE_FAIL.getName());
        }
        return vehicleMapper.updateById(vehicle);
    }

    /**
     * 车辆禁/启用
     */
    @Override
    public int stateChange(Vehicle vehicle) {
        Vehicle byId = vehicleMapper.selectById(vehicle.getId());
        if(byId.getState().equals(vehicle.getState())) {
            throw new ServiceException("操作失败");
        }
        return vehicleMapper.updateById(vehicle);
    }

    /**
     * 上传车辆
     */
    @Override
    public boolean upload(Long id) {
        // 平台信息
        PtQy ptQy = ptQyService.lists();
        Vehicle vehicle = vehicleMapper.selectById(id);
        if(vehicle != null) {
            if(!DictConstants.IS_SEND_TO_PRO.YSC.getKey().equals(vehicle.getIsSendToPro())) {
                if(DictConstants.IS_CERT.YRZ.getKey().equals(vehicle.getIsCert())) {
                    boolean b = WlhyUpload.wlhyUpload(ptQy, vehicle);
                    vehicle.setIsSendToPro(DictConstants.IS_SEND_TO_PRO.YSC.getKey());
                    int i = vehicleMapper.updateById(vehicle);
                    return b && i > 0;
                } else {
                    throw new ServiceException("车辆未认证，不可上传");
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

}
